//
//  TypeDetailViewController.m
//  Travel_M_Travel
//
//  Created by dlios on 15-3-25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TypeDetailViewController.h"
#import "NetworkingHandler.h"
#import "ListCell.h"
#import "DetailViewController.h"
#import "SDRefreshStart.h"
@interface TypeDetailViewController ()<UITableViewDataSource, UITableViewDelegate,SDRefreshStartDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UISegmentedControl *segmentedControl;
@property (nonatomic , retain)NSString *typeSeg;
@end

@implementation TypeDetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.page = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.navigationController setNavigationBarHidden:NO];
    
    self.title = self.mds.tname;
    

    NSArray *segmentedArray = [[NSArray alloc] initWithObjects:@"最火", @"最近更新", @"经典", nil];
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];
    self.segmentedControl.frame = CGRectMake(25, 64, self.view.frame.size.width*5/6, 30);
    [_segmentedControl addTarget:self action:@selector(segmentAction:)
               forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentedControl];
    [_segmentedControl release];
    
    
    UIView *viewS = [[UIView alloc] initWithFrame:CGRectMake(0, 94, self.view.frame.size.width, self.view.frame.size.height - 94)];
    viewS.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewS];
    [viewS release];
    
    NSString *str = [[@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name=" stringByAppendingString:self.mds.tname]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        NSLog(@"%@", resultDic);
        self.arr = [NSMutableArray array];
        NSArray *list = [resultDic objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            List *list = [[List alloc] initWithDic:dic];
            [self.arr addObject:list];
            [list release];
        }
        [_tableView reloadData];
    }];
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 0, self.view.frame.size.width-10, self.view.frame.size.height-50-94)  style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [viewS addSubview:self.tableView];
    [_tableView release];
    
    [SDRefreshStart SDRefeshTableView:self.tableView delegate:self];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse1";
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[ListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    List *list = [self.arr objectAtIndex:indexPath.row];
    cell.list = list;
    return cell;
}


- (void)segmentAction:(UISegmentedControl *)segment
{
    
    if (0 == segment.selectedSegmentIndex) {
        NSString *str = [[@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name=" stringByAppendingString:self.mds.tname]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
            NSLog(@"%@", resultDic);
            self.arr = [NSMutableArray array];
            NSArray *list = [resultDic objectForKey:@"list"];
            for (NSDictionary *dic in list) {
                List *list = [[List alloc] initWithDic:dic];
                [self.arr addObject:list];
                [list release];
            }
            [_tableView reloadData];
        }];
        self.typeSeg = @"hot";
    }else if(1 == segment.selectedSegmentIndex){

        
        NSString *str = [[@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=recent&device=iPhone&page=1&per_page=20&status=0&tag_name=" stringByAppendingString:self.mds.tname] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
            self.arr = [NSMutableArray array];
            NSArray *list = [resultDic objectForKey:@"list"];
            for (NSDictionary *dic in list) {
                List *list = [[List alloc] initWithDic:dic];
                [self.arr addObject:list];
                [list release];
            }
            [_tableView reloadData];
        }];

        self.typeSeg = @"recent";
 
    }else if (2 == segment.selectedSegmentIndex){

    
        NSString *str = [[@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=classic&device=iPhone&page=1&per_page=20&status=0&tag_name=" stringByAppendingString:self.mds.tname] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
            self.arr = [NSMutableArray array];
            NSArray *list = [resultDic objectForKey:@"list"];
            for (NSDictionary *dic in list) {
                List *list = [[List alloc] initWithDic:dic];
                [self.arr addObject:list];
                [list release];
            }
            [_tableView reloadData];
        }];
        self.typeSeg = @"classic";

    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.list = [_arr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:NO];
//    NSLog(@"~~~~~~~~%@~~~~~~~~~", detailVC.list);

}



-(void)headRefesh
{
    
    self.page = 1;
    [self.arr removeAllObjects];

    NSString *str = [[NSString stringWithFormat: @"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=%@&device=iPhone&page=1&per_page=20&status=0&tag_name=%@" ,self.typeSeg, self.mds.tname ] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        NSLog(@"%@", resultDic);
        self.arr = [NSMutableArray array];
        NSArray *list = [resultDic objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            List *list = [[List alloc] initWithDic:dic];
            [self.arr addObject:list];
            [list release];
        }
        [_tableView reloadData];
    }];

}

- (void)footRefesh
{
    self.page++;
    NSString *pageCount = [NSString stringWithFormat:@"%ld", self.page];
    
    
    NSString *str = [[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=%@&device=iPhone&page=%@&per_page=20&status=0&tag_name=%@", self.typeSeg, pageCount, self.mds.tname] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        NSLog(@"%@", resultDic);
        self.arr = [NSMutableArray array];
        NSArray *list = [resultDic objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            List *list = [[List alloc] initWithDic:dic];
            [self.arr addObject:list];
            [list release];
        }
        [_tableView reloadData];
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
